package org.awa.demo.opencv.gltf.newhandler;

import lombok.extern.slf4j.Slf4j;
import org.awa.demo.opencv.gltf.Constant;
import org.awa.demo.opencv.gltf.SoundUtil;
import org.locationtech.jts.math.Vector3D;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

//y固定面
@Slf4j
@Component
public class NewWallYHandler implements INewRectangleHandler {
    @Override
    public List<Vector3D> getRectanglePointList(List<Vector3D> pointList) {
        //根据墙面特性，因为是平面，所以XY的坐标系肯定是同时最大同时最小，不然就成曲面了，而Z轴代表高度。
        // 所以根据x的最大最小值可以直接拿到对应y的值。
        // 面的四个点分别是[minX,minY,minZ],[maxX,maxY,minZ],[minX,minY,maxZ],[maxX,maxY,maxZ]
        double normalY = pointList.get(0).getY();
        double maxX = Constant.MIN_FLOAT;
        double minX = Constant.MAX_FLOAT;
        double maxZ = Constant.MIN_FLOAT;
        double minZ = Constant.MAX_FLOAT;
        for (int i = 0; i < pointList.size(); i++) {
            Vector3D point  = pointList.get(i);
            double x = point.getX();
            double z = point.getZ();
            if(maxX < x){
                maxX = x;
            }
            if(minX > x){
                minX = x;
            }
            if(maxZ < z){
                maxZ = z;
            }
            if(minZ > z){
                minZ = z;
            }
        }

        List<Vector3D> resultList = new ArrayList<>();
        resultList.add(new Vector3D(minX,normalY,minZ));
        resultList.add(new Vector3D(maxX,normalY,minZ));
        resultList.add(new Vector3D(maxX,normalY,maxZ));
        resultList.add(new Vector3D(minX,normalY,maxZ));

        return resultList;
    }

    //网格化并获取各网格中心点
    @Override
    public List<Vector3D> getGridObject(List<Vector3D> pointList, int widthSize, int heightSize) {
        double normalY = pointList.get(0).getY();
        double[] xArr = SoundUtil.getPointListByLine(pointList.get(0).getX(),pointList.get(1).getX(),widthSize);
        double[] zArr = SoundUtil.getPointListByLine(pointList.get(1).getZ(),pointList.get(2).getZ(),heightSize);

        List<Vector3D> resultList = new ArrayList<>();

        for (int i = 0; i < xArr.length; i++) {
            double x = xArr[i];
            for (int j = 0; j < zArr.length; j++) {
                double z = zArr[j];
                resultList.add(new Vector3D(x,normalY,z));
            }
        }

        return resultList;
    }
}
